package com.kinghood.productcenter.controller.base;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.kinghood.infrastructure.api.authority.SysLogModuleEnum;
import com.kinghood.infrastructure.aspect.OperatorLog;
import com.kinghood.infrastructure.model.KingHoodResponseVO;
import com.kinghood.infrastructure.model.constant.CommonConstants;
import com.kinghood.infrastructure.model.dto.SortReqDTO;
import com.kinghood.infrastructure.model.dto.StatusReqDTO;
import com.kinghood.productcenter.common.model.dao.base.BaseSupplierDO;
import com.kinghood.productcenter.common.model.dto.base.ListSupplierQueryDTO;
import com.kinghood.productcenter.common.model.dto.base.SupplierReqDTO;
import com.kinghood.productcenter.service.base.IBaseSupplierService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 基础选项-供应商 前端控制器
 * </p>
 *
 * @author KingHood
 * @since 2023-12-20
 */
@RestController
@RequestMapping("/baseSupplier")
public class BaseSupplierController {
    @Resource
    private IBaseSupplierService supplierService;

    /**
     * 列表
     *
     * @author jp
     * @date 2023/12/20
     */
    @PostMapping("/listSupplier")
    public KingHoodResponseVO listSupplier(@RequestBody ListSupplierQueryDTO param) {
        return KingHoodResponseVO.success(supplierService.listSupplier(param));
    }

    /**
     * 供应商下拉列
     *
     * @return 供应商
     * @author zfj
     * @date 2024/1/6
     */
    @PostMapping("/getSupplierList")
    public KingHoodResponseVO<List<BaseSupplierDO>> getSupplierList() {
        List<BaseSupplierDO> list = new LambdaQueryChainWrapper<>(supplierService.getBaseMapper())
                .eq(BaseSupplierDO::getIsDelete, CommonConstants.IS_DELETE_N)
                .eq(BaseSupplierDO::getStatus, CommonConstants.ENABLE)
                .list();
        return KingHoodResponseVO.success(list);
    }

    /**
     * 新增更新
     *
     * @param param
     * @author jp
     * @date 2023/12/21
     */
    @OperatorLog(module = SysLogModuleEnum.Type.PRODUCT_SUPPLIER,content = "供应商管理-新增、修改供应商")
    @PostMapping("/addOrUpdateSupplier")
    public KingHoodResponseVO addOrUpdateSupplier(@Valid @RequestBody SupplierReqDTO param) {
        supplierService.addOrUpdateSupplier(param);
        return KingHoodResponseVO.success();
    }

    /**
     * 根据id删除
     *
     * @param id id
     * @author jp
     * @date 2023/12/21
     */
    @OperatorLog(module = SysLogModuleEnum.Type.PRODUCT_SUPPLIER,content = "供应商管理-删除供应商")
    @RequestMapping("/delSupplier/{id}")
    public KingHoodResponseVO delSupplier(@PathVariable("id") Long id) {
        supplierService.delSupplierById(id);
        return KingHoodResponseVO.success();
    }

    /**
     * 根据ID查询详情
     *
     * @param id id
     * @author jp
     * @date 2023/12/21
     */
    @GetMapping("/detailSupplier/{id}")
    public KingHoodResponseVO detailSupplier(@PathVariable("id") Long id) {
        return KingHoodResponseVO.success(supplierService.detailSupplier(id));
    }

    /**
     * 更新供货商状态
     *
     * @param param 参数
     * @author jp
     * @date 2023/12/21
     */
    @OperatorLog(module = SysLogModuleEnum.Type.PRODUCT_SUPPLIER,content = "供应商管理-修改状态")
    @PostMapping("/updateSupplierStatus")
    public KingHoodResponseVO updateSupplierStatus(@Valid @RequestBody StatusReqDTO param) {
        supplierService.updateSupplierStatus(param);
        return KingHoodResponseVO.success();
    }

    /**
     * 更新供货商排序
     *
     * @param param 参数
     * @author jp
     * @date 2023/12/21
     */
    @OperatorLog(module = SysLogModuleEnum.Type.PRODUCT_SUPPLIER,content = "供应商管理-修改排序")
    @PostMapping("/updateSupplierSort")
    public KingHoodResponseVO updateSupplierSort(@Valid @RequestBody SortReqDTO param) {
        supplierService.updateSupplierSort(param);
        return KingHoodResponseVO.success();
    }

    /**
     * 下载
     *
     * @param param 参数
     * @author jp
     * @date 2024/01/04
     */
    @PostMapping("/downloadSupplier")
    public void downloadSupplier(@RequestBody ListSupplierQueryDTO param, HttpServletResponse response) {
        supplierService.downloadSupplier(param, response);
    }
}
